package dk.aiszone.lang.autosource;

import static org.junit.Assert.assertEquals;
import japa.parser.ParseException;

import org.junit.Test;

import dk.aiszone.lang.autosource.generatorobjects.Annotation;
import dk.aiszone.lang.autosource.generatorobjects.Argument;
import dk.aiszone.lang.autosource.generatorobjects.Class;
import dk.aiszone.lang.autosource.generatorobjects.ClassBody;
import dk.aiszone.lang.autosource.generatorobjects.Declaration;
import dk.aiszone.lang.autosource.generatorobjects.GenericClass;
import dk.aiszone.lang.autosource.generatorobjects.JavaFile;
import dk.aiszone.lang.autosource.generatorobjects.Method;
import dk.aiszone.lang.autosource.generatorobjects.Scope;
import dk.aiszone.lang.autosource.generatorobjects.StaticArea;
import dk.aiszone.lang.autosource.generatorobjects.Variable;
import dk.aiszone.lang.autosource.generatorobjects.GenericClass.Operator;

/**
 * Test cases for the <code>SoureFileParser</code>
 * 
 * @author Ais
 */
public class ParserTest {

    private static final JavaFile TEST01;
    static {
        Method test01 = new Method(new Declaration(Scope.PUBLIC), Class.VOID, "test01");
        test01.addArgument(new Argument(new Class("Boolean"), "test"));
        test01.appendToBody("int i = 0;\n");
        test01.appendToBody("if(test == true) {\n");
        test01.appendToBody("i = 1;\n");
        test01.appendToBody("} else if(test == false) {\n");
        test01.appendToBody("i = 2;\n");
        test01.appendToBody("} else {\n");
        test01.appendToBody("i = 3;\n");
        test01.appendToBody("}\n");
        test01.appendToBody("System.out.println(\"Test\");");
        Annotation test = new Annotation(new Class("Test"));
        test.addValue("expected", "RuntimeException");

        Annotation value = new Annotation(new Class("Join"));
        value.addStringValue("table", "test");

        test.addValue("value", value);
        test01.addAnnotation(test);

        Variable globalVariable = new Variable(new Declaration(Scope.PRIVATE, true, true, false), new Class("String"), "GLOBAL_VARIABLE", "\"Global Me\"");
        Variable holdMe = new Variable(new Declaration(Scope.PRIVATE, false, true, false), new Class("String"), "HOLD_ME");
        Variable a = new Variable(new Declaration(Scope.PRIVATE), new Class("boolean"), "a");
        Variable b = new Variable(new Declaration(Scope.PRIVATE), new Class("boolean"), "b", "false");
        Variable c = new Variable(new Declaration(Scope.PRIVATE), new Class("boolean"), "c");

        StaticArea staticArea = new StaticArea();
        staticArea.appendToBody("Map<String, String> map = new HashMap<String, String>();\n");
        staticArea.appendToBody("for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext(); ) {\n");
        staticArea.appendToBody("type type = (type) iterator.next();\n");
        staticArea.appendToBody("}");

        // public abstract class Test01<K, A extends Object> extends Object<Map<String, List<String>>, Boolean> {
        ClassBody body = new ClassBody(new Declaration(Scope.PUBLIC, false, true, false), "Test01");
        body.addGenerics(new GenericClass("K"));
        body.addGenerics(new GenericClass("A", Operator.EXTENDS, new Class("Object")));

        Class _extends = new Class("Object");
        _extends.addGeneric(new GenericClass(new Class("java.util.Map", new GenericClass(new Class("String")), new GenericClass(new Class("java.util.List", new GenericClass(new Class("String")))))));
        _extends.addGeneric(new GenericClass(new Class("Boolean")));
        body.setExtendsClass(_extends);

        body.addVariable(globalVariable);
        body.addVariable(holdMe);
        body.addVariable(a);
        body.addVariable(b);
        body.addVariable(c);
        body.addStaticArea(staticArea);
        body.addMethod(test01);

        TEST01 = new JavaFile("dk.aiszone.lang.autosource", body);
    }

    /**
     * Simple test case working on the file <i>Test01.java</i>.
     * 
     * @throws ParseException
     *             Thrown the the parser fails
     */
    @Test
    public void test01() throws ParseException {
        JavaFile javafile = new SourceFileParser("Testdata/autosource/Test01.java").getResultingJavaFile();

        assertEquals(TEST01, javafile);
    }
}